using UnityEngine;

public class PlayerState : ScriptableObject, IState
{
    /********变量*********/
    float stateStartTime;
    [SerializeField] private string stateName;
    [SerializeField,Range(0,1)] private float transitionDuration = 0.1f;
    protected int stateHash;
    /*********************************************隔离线**********************************************************/
    /*************标准组件*************/
    protected Rigidbody2D my_Body2D;                                        // 刚体组件，用于获取物体刚体属性
    protected Animator animator;                                            // 动画组件，用来播放动画
    protected BoxCollider2D playerBox2D;                                    // 玩家的碰撞检测盒子
    /*************控制组件*************/
    protected PlayerControl playerControl;                                  // 控制玩家移动
    protected GameObject playerFoot;                                        // 玩家的脚
    protected PlayerStatusInformation playerStatusInformation;              // 玩家状态信息
    /*************输入检测*************/
    protected PlayerInput playerInput;                                      // 输入检测

    protected PlayerDetection playerDetection;                              // 射线检测
    /*********************************************隔离线**********************************************************/
    protected PlayerStateMachine stateMachine;                            //PlayerStateMachine，玩家状态机类，执行状态间的切换
    /*************相关信息*************/
    /****动画检测*****/
    protected bool isAnimationFinished => stateDuration >= animator.GetCurrentAnimatorStateInfo(0).length;          //当前动画是否播放完毕
    protected float stateDuration => Time.time - stateStartTime;
    /*********************************************隔离线**********************************************************/
    /*******组件传递*******/
    public void Initiatize(
        Animator animator, Rigidbody2D my_Body2D, BoxCollider2D playerBox2D,
        PlayerControl playerControl, PlayerStatusInformation playerStatusInformation, GameObject playerFoot,
        PlayerInput playerInput, PlayerDetection playerDetection,
        PlayerStateMachine stateMachine)
        {
            // 组件
            this.animator = animator;
            this.my_Body2D = my_Body2D;
            this.playerBox2D = playerBox2D;
            // 控制组件
            this.playerControl = playerControl;
            this.playerStatusInformation = playerStatusInformation;
            this.playerFoot = playerFoot;
            // 输入
            this.playerInput = playerInput;
            this.playerDetection = playerDetection;
            // 状态机
            this.stateMachine = stateMachine;
        }
        private void OnEnable()
        {
            stateHash = Animator.StringToHash(stateName);
        }
        public virtual void Enter()
        {
            animator.CrossFade(stateHash,transitionDuration);
            stateStartTime = Time.time;
        }

        public virtual void Exit()
        {
            //
        }

        public virtual void LogicUpdate()
        {
            //
        }

        public virtual void PhysicalUpdate()
        {
            //
        }
    }